// pages/home/vote/components/home/home.js
const app = getApp();
const util = require('../../../../../utils/util')
var { _get_member_apply_vote_list } = require('../../../../../utils/xiaohuangyu')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    activityDetail: { // 属性名
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    searchValue: '',
    applyVoteList: [],
    totalPage: 1,
    remainingDate: {},
    queryParams: {
      currentPage: 1,
      number: '',
    },
    swiperList: [{
      id: 0,
      type: 'image',
      url: '../../../../../images/banner1.jpg'
    }, {
      id: 1,
      type: 'image',
      url: '../../../../../images/banner2.jpg'
    }, {
      id: 2,
      type: 'image',
      url: '../../../../../images/banner1.jpg'
    }],
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      this.get_member_apply_vote_list(this.data.queryParams)
      // 计算倒计时
      this.setData({
        remainingDate: util.getRemainingDate(this.data.activityDetail.start_time, this.data.activityDetail.end_time)
      })
    },
    moved: function () {
      console.log('moved')
    },
    detached: function () {
      console.log('detached')
    },
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      console.log('show')
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onReachHomeBottom: function () {
      // 父组件上拉触发，上拉刷新
      if (this.data.queryParams.currentPage < this.data.totalPage) {
        this.setData({
          [`queryParams.currentPage`]: this.data.queryParams.currentPage + 1
        })
        this.get_member_apply_vote_list(this.data.queryParams)
      } else {
        wx.showToast({
          title: '没有更多的数据了',
          icon: 'none'
        })
      }
    },
    // 查询
    searchInputBlur(res) {

      this.setData({
        [`queryParams.currentPage`]: 1,
        [`queryParams.number`]: res.detail.value ? Number(res.detail.value) : '',
      })
    },
    searchTap() {
      // console.log(this.data.queryParams)
      this.setData({
        applyVoteList: []
      })
      this.get_member_apply_vote_list(this.data.queryParams)
    },
    get_member_apply_vote_list(params) {
      _get_member_apply_vote_list(this.data.activityDetail.id, params, res => {
        console.log('_get_member_apply_vote_list', res)
        if (res.status) {
          this.setData({
            applyVoteList: this.data.applyVoteList.concat(res.data.dataList),
            [`queryParams.currentPage`]: res.data.currentPage,
            totalPage: res.data.totalPage
          })
        }
      })
    },
    voteDetailTap(res) {
      // 回调
      // 向父组件传值，跳转指定详情
      this.triggerEvent('voteDetailTap', {
        params: {
          number: res.currentTarget.dataset.number,
        }
      })
    }
  }
})
